package com.xxxx.portal.controller;

import com.xxxx.api.pojo.Admin;
import com.xxxx.api.pojo.Cart;
import com.xxxx.api.pojo.CartResult;
import com.xxxx.api.pojo.ResultInfo;
import com.xxxx.api.service.CartService;
import com.xxxx.api.service.IRegisterService;
import com.xxxx.api.utils.LoginUserUtil;
import com.xxxx.portal.pojo.CartParams;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * cart控制器
 *
 * @author nathan-wang on 2020-11-02
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Reference(version = "1.0")
    private CartService cartService;
    @Reference(version = "1.0")
    private IRegisterService registerService;
    /**
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/addCart")
    @ResponseBody
//    public ResultInfo addCart(HttpServletRequest request, @RequestBody CartParams cart) {
    public ResultInfo addCart(HttpServletRequest request) {
        //userId
        Integer userid = LoginUserUtil.releaseUserIdFromCookie(request);
        String userId = String.valueOf(userid);
        //goodId,goodNum
//        Integer goodsid = cart.getGoodsId();
//        String goodsId = String.valueOf(goodsid);
        String goodsId = request.getParameter("goodsId");
//        Integer goodsnum = cart.getGoodsNum();
//        String goodsNum = String.valueOf(goodsnum);
        String goodsNum = request.getParameter("goodsNum");
        ResultInfo resultInfo = cartService.addCart(userId, goodsId, goodsNum);
        return resultInfo;
    }

    /**
     * getCartList;跳转页面list.ftl
     * @param request
     *
     * @return
     */
    @RequestMapping("/getCartList")
    public String toPageList(HttpServletRequest request, Model model){
        //userId
        int userid = LoginUserUtil.releaseUserIdFromCookie(request);
        short id = (short) userid;
        //admin
        Admin admin = registerService.queryAdminById(id);
        //cartList
        CartResult cartResult = cartService.getCartList(admin);
        //前端名字对应cartResult
        CartResult cartResult1 = new CartResult(null,new BigDecimal("0.0"));
        model.addAttribute("cartResult",null==cartResult?new CartResult():cartResult);
        return "cart/list";
    }

    /**
     * getCartNum
     * @param request
     * @return
     */
    @RequestMapping("/getCartNum")
    public Integer getCartNum(HttpServletRequest request){
        //userId
        int userid = LoginUserUtil.releaseUserIdFromCookie(request);
        short id = (short) userid;
        //admin
        Admin admin = registerService.queryAdminById(id);
        Integer cartNum = cartService.getCartNum(admin);
        return cartNum;
    }



    /**
     * 从redis里面获取所有的商品数量
     * @param request
     * @return
     *博文调用
    **/
}




